import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';

enum Api {
  page = "/system-service/resource/pageResource",
  list = "/system-service/resource/listResource",
  save = "/system-service/resource/saveResource",
  edit = "/system-service/resource/updateResourceById",
  get = "/system-service/resource/getResourceById",
  delete = "/system-service/resource/deleteResourceById",
  deleteBatch = "/system-service/resource/deleteResourceByIds",
  syncResource = "/system-service/resource/syncResource",
}

export interface RamResource {
  url: string;
  action: string;
  actionType: string;
  name: string;
}

export const pageResource = (params) => {
  return defHttp.get({ url: Api.page, params });
}

export const listResource = (params) => {
  return defHttp.get<RamResource[]>({ url: Api.list, params });
}


export const saveOrUpdateResource = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
}

export const getResourceById = (params) => {
  return defHttp.get({ url: Api.get, params });
}


export const syncResource = () => {
  return defHttp.post({ url: Api.syncResource, timeout: 20000000 });
}

export const deleteResource = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

export const batchDeleteResource = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}
